﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;

using System.Collections;
using Microsoft.Win32;
using System.IO;
using Cassini;


namespace IbnBazStartup
{
    public partial class Form1 : Form
    {
        //private Container components;



        private int nGenPort;



        private ArrayList oCassiniList;

        private Container components;


        public Form1()
        {
            this.nGenPort = 0xbf68;
            this.components = null;
            this.InitializeComponent1();
            this.oCassiniList = new ArrayList();
        }







        private void CheckRegister()
        {
            RegistryKey key;
            if (Registry.LocalMachine.OpenSubKey(@"Software\Harf\WebCDInit") == null)
            {
                key = Registry.LocalMachine.CreateSubKey(@"Software\Harf\WebCDInit");
            }
            key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
            if (key.GetValue("WebCDInit") != Application.ExecutablePath)
            {
                key.SetValue("WebCDInit", Application.ExecutablePath);
            }
        }



        //private void CheckRegister()
        //{
        //    RegistryKey key;
        //    if (Registry.LocalMachine.OpenSubKey(@"Software\Harf\WebCDInit") == null)
        //    {
        //        key = Registry.LocalMachine.CreateSubKey(@"Software\Harf\WebCDInit");
        //    }
        //    key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
        //    if (key.GetValue("WebCDInit") != Application.ExecutablePath)
        //    {
        //        key.SetValue("WebCDInit", Application.ExecutablePath);
        //    }
        //}



        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }




        private int FindPort()
        {
            while (this.nGenPort < 0xbfff)
            {
                if (this.PortIsFree(this.nGenPort))
                {
                    return this.nGenPort++;
                }
                this.nGenPort++;
            }
            return -1;
        }




        private void Form1_Load(object sender, EventArgs e)
        {
            this.CheckRegister();
            RegistryKey key2 = Registry.LocalMachine.OpenSubKey(@"Software\Harf\WebCDInit", true);
            foreach (string str2 in key2.GetSubKeyNames())
            {
                RegistryKey key = key2.OpenSubKey(str2, true);
                string path = (string)key.GetValue("Path");
                if (Directory.Exists(path))
                {
                    key.SetValue(null, this.GetFreePort(path).ToString());
                }
                else
                {
                    key.SetValue(null, "");
                }
            }
            key2.Close();
            base.Visible = false;
            base.WindowState = FormWindowState.Minimized;
        }




        private int GetFreePort(string strPath)
        {
            
            int port = this.FindPort();
            Server server = new Server(port, "/", strPath);
            server.Start();
            this.oCassiniList.Add(server);
            return port;
        }




        private void InitializeComponent1()
        {
            this.AutoScaleBaseSize = new Size(5, 13);
            base.ClientSize = new Size(0x178, 0xed);
            base.MaximizeBox = false;
            base.Name = "Form1";
            base.ShowInTaskbar = false;
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Form1";
            base.WindowState = FormWindowState.Minimized;
            base.Load += new EventHandler(this.Form1_Load);
        }


        private bool PortIsFree(int Port)
        {
            TcpClient client = new TcpClient();
            try
            {
                client.Connect("localhost", Port);
                return false;
            }
            catch
            {
                return true;
            }
        }

 

    }
}
